// pages/spelt/spelt.js
//获取应用实例
const app = getApp();

Page({
  data: {
    /*页头选择 */
    head_num: 0,
    //下拉判断
    pull_down_judge: true,
    scrollTop: 0,
    // 订单列表
    lists: '',

    //num对应页头的分类  
    //sort状态的分类  1为等待付款  2为团人数不足  3为成团待发货  4为成团已发货
    //sort状态 2 的 total 为组团的总人数， already 为已参加的人数
    all: [
      //待付款
    ],
    imgurl: app.urlimg,
    defaultpages: "2",
    loadHidden: true,
    loadText: "加载中...",
    toastHidden: true,
    msg: '没有更多订单了',
    //猜你喜欢
    love_goods: true,
  },
  //页头点击函数
  head_click: function (e) {
    var that = this;
    that.setData({
      head_num: e.currentTarget.dataset.index,
      scrollTop: 0,
    })
    that.load(e.currentTarget.dataset.index);
  },
  //提醒商家发货
  reminding: function (e) {
    //数据加载
    // wx.request({
    // 	url: '',
    // 	method: 'GET',
    // 	headers: {
    // 		'Content-Type': 'application/json'
    // 	},
    // 	success: function (res) {
    //提示信息
    wx.showToast({
      title: '已提醒卖家发货~',
      icon: 'success',
      duration: 1200,
    })
    // 	},
    // 	//接口调用失败返回的函数
    // 	fail: function (err) {
    // 		console.log(err)
    // 	}
    // })
  },
  //查看物流
  logistics: function (e) {
    wx.navigateTo({
      url: '/pages/account/logistics/index?id=' + e.currentTarget.dataset.order_id,
    })
  },
  //退款申请
  refunds: function (e) {
    wx.navigateTo({
      url: '/pages/account/refund/index?order_goods_id=' + e.currentTarget.dataset.id + '&order_id=' + e.currentTarget.dataset.order_id,
    })
  },
  //确认收货
  sure_goods: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '请收到货后，再确认收货！',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.url + 'api_user/order_status',
            data: {
              order_id: e.currentTarget.dataset.order_id,
              op: 'confirm'
            },
            header: {
              'content-type': 'application/json',
              'token': wx.getStorageSync('token_key')
            }, success: function (json) {
              if (json.data.done == true) {
                // that.getData();
                console.log(json);
              } else {
                wx.showModal({
                  title: '提示',
                  content: json.data.message,
                })
              }
            }
          })
        }
      }
    })
  },
  // getData: function () {
  // 	var that = this;
  // 	that.setData({ loadHidden: false })
  // 	wx.request({
  // 		url: app.url + 'api_user/orderV1',
  // 		data: {
  // 			status: that.data.selectIndex
  // 		},
  // 		header: {
  // 			'content-type': 'application/json',
  // 			'token': wx.getStorageSync('token_key')
  // 		}, success: function (json) {
  // 			console.log(json);
  // 			if (json.data.done == true) {
  // 				that.setData({ lists: json.data.result, loadHidden: true })
  // 				console.log(that.data.data);
  // 			} else {
  // 				wx.showModal({
  // 					title: '提示',
  // 					content: json.data.message,
  // 				})
  // 			}
  // 		}, complete: function (json) {

  // 		}
  // 	})
  // },
  //查看订单详情
  purchase: function (e) {
    //查看状态   2为团人数不足  3为成团待发货  5为拼团失败退款中  6为拼团失败退款成功
    var order_id = e.currentTarget.dataset.order_id
    if (order_id > 0) {
      wx.navigateTo({
        url: '../../pages/mygroup/order/order?orderId=' + order_id,
      })
    }
  },
  //查看团详情
  // groupdetail: function (e) {
  //     var order_id = e.currentTarget.dataset.order_id
  //     if (order_id > 0) {
  //         wx.navigateTo({
  //             url: '../../pages/mygroup/mygroupdetail/mygroupdetail?order_id=' + order_id,
  //         })
  //     }
  // },
  /**
   * 查看中奖详情
   */
  prizeTap: function (e) {
    var order_id = e.currentTarget.dataset.order_id
    if (order_id > 0) {
      wx.navigateTo({
        url: '/pages/mygroup/winning/winning?orderId=' + order_id,
      })
    }
  },

  //邀请好友拼团
  invite: function (e) {
    var order_id = e.currentTarget.dataset.order_id
    if (order_id > 0) {
      wx.navigateTo({
        url: '../../pages/mygroup/invite/invite?orderId=' + order_id,
      })
    }
  },
  //页面加载
  onLoad: function (options) {
    var that = this;
    var num = 0;
    if (options.fid == 2) {
      num = 1;
      that.setData({
        head_num: 1,
      })
    }


    app.app_load(load_fn);

    function load_fn() {
      wx.showLoading({
        title: '加载中...',
      });
      that.load(num);


      //猜你喜欢
      wx.request({
        url: app.url + 'api_footprint/footprint_group_list',
        data: {
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        },
        success: function (json) {
          that.setData({
            likegoods: json.data.result,
          })
        }
      })
    }
  },

  // 猜你喜欢列表
  click_goods: function (e) {
    wx.navigateTo({
      url: '/pages/index/detail/index?id=' + e.currentTarget.id,
    })
  },
  /**
  * 页面上拉触底事件的处理函数
  */
  // onReachBottom:
  pull_down:
  function () {
    var that = this;
    var num = this.data.head_num;
    // console.log(num)
    var team_status = '';
    if (num == 0) {
      team_status = 0
    } else if (num == 1) {
      team_status = 1
    } else if (num == 2) {
      team_status = 2
    } else if (num == 3) { //待开奖
      team_status = 3
    } else if (num == 4) { //拼团失败
      team_status = 4
    }
    //console.log(that.data.defaultpages);
    if (that.data.pull_down_judge) {
      that.setData({ pull_down_judge: false, })
      var page = that.data.defaultpages;
      wx.request({
        url: app.url + 'api_team/myGroupOrder',
        data: {
          page: page,
          team_status: team_status,
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        },
        success: function (res) {
          console.log('下拉成功');
          console.log(res);
          if (res.data.result.length > 0) {
            var lists = that.data.lists;
            for (var i = 0; i < res.data.result.length; i++) {
              lists.push(res.data.result[i]);
            }
            if (lists < 5) {
              that.setData({ love_goods: false, });
            }
            that.setData({
              lists: lists,
            });
            that.setData({ defaultpages: ++that.data.defaultpages, pull_down_judge: true, });
          } else {
            wx.showToast({
              title: '暂无更多数据',  //标题  
              icon: 'loading',  //图标，支持"success"、"loading"   
              duration: 1200,
            })
            that.setData({ pull_down_judge: true, love_goods: false, })
          }

        }, complete: function () {
          //显示出加载中的提示
          that.setData({ loadHidden: true })
        }

      })
    }

  },
  onShow: function () {
  },
  /*数据加载*/
  load: function (num) {
    console.log(num);
    var that = this;
    that.setData({
      defaultpages: "2",   //显示全部帖子的页码
    })
    //数据加载
    var that = this;
    //加载全部订单
    wx.request({
      url: app.url + 'api_team/myGroupOrder',
      method: 'GET',
      data: {
        team_status: num,
      },
      header: {
        'Content-Type': 'application/json',
        'token': wx.getStorageSync('token_key')
      },
      success: function (res) {
        wx.hideLoading();
        console.log(res.data.result);
        for (var i = 0; i < res.data.result.length; i++) {
          if (res.data.result[i].team_sign.indexOf("pt-") > -1) {
            res.data.result[i].team_type_text = '拼团';
          }
          if (res.data.result[i].team_sign.indexOf("cjtm") > -1) {
            res.data.result[i].team_type_text = '会员抽奖团';
          }
          if (res.data.result[i].team_sign.indexOf("cjt") > -1) {
            res.data.result[i].team_type_text = '抽奖团';
          }
          if (res.data.result[i].team_sign.indexOf("mdt") > -1) {
            res.data.result[i].team_type_text = '免单团';
          }
        }
        that.setData({
          lists: res.data.result,
          love_goods: true,
        })

        if (res.data.result == '' || res.data.result.length < 5) {
          that.setData({
            love_goods: false,
          })
        }
      },
      //接口调用失败返回的函数
      fail: function (err) {
        console.log(err)
      }
    })
  },
})
